﻿using System.Web.Routing;
using Isis.Parsers.Interfaces;

namespace Isis.Parsers.Impl
{
    public class RouteDataParser : IRouteDataParser
    {
        public string GetController(RequestContext requestContext)
        {
            return GetRouteValue(requestContext, "controller");
        }

        public string GetAction(RequestContext requestContext)
        {
            return GetRouteValue(requestContext, "action");
        }

        public string GetParameter(RequestContext requestContext)
        {
            return GetRouteValue(requestContext, "parameter");
        }

        public string GetRouteValue(RequestContext requestContext, string routingName)
        {
            if (requestContext == null || requestContext.RouteData == null || requestContext.RouteData.Values == null)
            {
                return string.Empty;
            }
            if (requestContext.RouteData.Values.ContainsKey(routingName))
            {
                return requestContext.RouteData.Values[routingName].ToString();
            }
            return string.Empty;
        }
    }
}